package com.ruoyi.project.user.host.service.impl;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import com.ruoyi.project.user.host.domain.Host;
import com.ruoyi.project.user.host.mapper.HostMapper;
import com.ruoyi.project.user.host.service.HostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class HostServiceImpl implements HostService {
    @Autowired
    private HostMapper hostMapper;

    @Autowired
    private IUserService iUserService;
    //主持人岗位在数据库中的主键值
    @Value("${my.mysql.post.hostid}")
    private Long hostId;

    @Override
    public List<Host> showHost(Host host) {
        return hostMapper.selectByHost(host);
    }

    @Override
    public Long checkPhone(String hostPhone) {
        return hostMapper.selectByHostphone(hostPhone);
    }

    @Override
    public Long checkEmail(String hostEmail) {
        return hostMapper.selectByHostemail(hostEmail);
    }

    @Transactional
    @Override
    public int insert(Host host, String hostPassword) {
        Date date = new Date();
        User user = new User();
        user.setPassword(hostPassword);
        user.setEmail(host.getHostEmail());
        user.setPhonenumber(host.getHostPhone());
        user.setLoginName(host.getHostPhone());
        user.setUserName(host.getHostPhone());
        user.setCreateTime(date);
        Long [] postIds = {hostId};
        user.setPostIds(postIds);
        int index = iUserService.insertUser(user);
        if(index>0) {
            host.setHostUserId(user.getUserId());
            host.setHostRegtime(date);
            int index2 = hostMapper.insertHost(host);
            if(index2>0){
                return 1;
            }
        }
        return 0;
    }

    @Override
    public int updateSortweightByHostId(Short sortWeight, Long hostId) {
        return hostMapper.updateSortweightByHostId(sortWeight,hostId);
    }

    @Override
    public List<Host> export(Host host, int order) {
        return hostMapper.selectByHostWithOrder(host,order);
    }

    @Transactional
    @Override
    public int delete(Long[] ids) {
        int index = 0;
        for(Long id : ids){
            Host host = hostMapper.selectByHostid(id);
            index +=iUserService.deleteUserById(host.getHostUserId());
        }
        if (index==ids.length){
            int index2 = hostMapper.deleteByIds(ids);
            return index2;
        }

        return 0;
    }

    @Override
    public Host showHostById(Long hostId) {
        return hostMapper.selectByHostid(hostId);
    }

    @Transactional
    @Override
    public int updateHost(Host host) {
        int index = hostMapper.updateHostByHostId(host);
        if(index==1){
            Host selectHost = hostMapper.selectByHostid(Long.parseLong(host.getHostId()+""));
            User user = new User();
            user.setUserId(selectHost.getHostUserId());
            user.setPhonenumber(host.getHostPhone());
            user.setUserName(host.getHostPhone());
            user.setLoginName(host.getHostPhone());
            user.setEmail(host.getHostEmail());
            int index2 = iUserService.updateUser(user);
            if(index2==1){
                return index2;
            }
        }

        return 0;
    }

    @Transactional
    @Override
    public int updateState(Host host) {
        Host selectHost = hostMapper.selectByHostid((long) (int) host.getHostId());
        selectHost.getHostUserId();
        User user = new User();
        user.setUserId(selectHost.getHostUserId());
        user.setStatus(host.getHostState()==2?"1":"0");
        user.setUpdateBy(ShiroUtils.getLoginName());
        iUserService.updateUserInfo(user);
        return hostMapper.updateStateByHostid(host);
    }

    @Override
    public int updatePermissionsetByHostId(Host host) {
        return hostMapper.updatePermissionByHostId(host);
    }
}
